package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.Tool.HashTool;
import com.springboot.hiresystem.domain.Users;
import com.springboot.hiresystem.dto.req.UsersQueryRequestDTO;
import com.springboot.hiresystem.dto.req.UsersUpdateRequest;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.dto.resp.UsersResponseDTO;
import com.springboot.hiresystem.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {
    private UsersService usersService;

    public UsersController(UsersService usersService) {
        this.usersService = usersService;
    }

// 带分页和条件查询
    @GetMapping("/list")
public Result<PageResponse<UsersResponseDTO>> list(UsersQueryRequestDTO usersQueryRequestDTO) {

    // 查询条件
    QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(usersQueryRequestDTO.getUsername() != null, "username", usersQueryRequestDTO.getUsername());

    // 分页信息
    Page<Users> page = new Page<>(usersQueryRequestDTO.getPage(), usersQueryRequestDTO.getSize());
    // 分页查询
    Page<Users> usersPage = usersService.page(page, queryWrapper);
    // 获取结果集
    List<Users> users = usersPage.getRecords();
    log.info("查询结果:{}", users);

    // 日志输出总记录数和总页数
    log.info("总记录数:{},总页数:{}", usersPage.getTotal(), usersPage.getPages());

    PageResponse<UsersResponseDTO> pageResponse = new PageResponse<>();
    // 转换成DTO
    List<UsersResponseDTO> usersResponseDTOS = BeanUtil.copyToList(users, UsersResponseDTO.class);
    pageResponse.setTotal(usersPage.getTotal());
    pageResponse.setList(usersResponseDTOS);
    System.out.println("-----------------"+pageResponse.toString());

        // 返回结果
    return Result.success(pageResponse);
}

    // 条件查询
    @PostMapping("/query")
    public Result<List<UsersResponseDTO>> query(@RequestBody UsersQueryRequestDTO usersQueryRequestDTO) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();

        if (usersQueryRequestDTO.getUserId() != null) {
            queryWrapper.eq("user_id", usersQueryRequestDTO.getUserId());
        }
        if (usersQueryRequestDTO.getUsername() != null) {
            queryWrapper.like("username", usersQueryRequestDTO.getUsername());
        }
        if (usersQueryRequestDTO.getEmail() != null) {
            queryWrapper.like("email", usersQueryRequestDTO.getEmail());
        }
        List<Users> users = usersService.list(queryWrapper);
        List<UsersResponseDTO> usersResponseDTOS = BeanUtil.copyToList(users, UsersResponseDTO.class); // 使用 BeanUtil.copyToList

        return Result.success(usersResponseDTOS); // 自动设置 count
    }
    //更新
    @PostMapping("/update")
    public Result<String> update(@RequestBody UsersUpdateRequest usersUpdateRequest) {
        Users users = BeanUtil.copyProperties(usersUpdateRequest, Users.class);
        usersService.updateById(users);
        return Result.success();
    }
    //添加
    @PostMapping("/add")
    public Result<String> add(@RequestBody Users users) throws Exception {
        System.out.println("--------------add"+users.toString());
        //将users中的passwordHash转为哈希值
        users.setPasswordHash(HashTool.encrypt(users.getPasswordHash()));
        if (!usersService.save(users)){
            return Result.failure(500,"添加失败,用户名重复");
        }
        return Result.success("添加成功");
    }
    //按照id删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Users users) {
        usersService.removeById(users);
        return Result.success("删除成功");
    }
    //添加按照用户名和密码查询
    @PostMapping("/getUser")
    public Result<Users> queryByUsernameAndPassword(@RequestBody Users users) throws Exception {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        System.out.println(users.getUsername());
        System.out.println(users.getPasswordHash());
        queryWrapper.eq("username", users.getUsername());
        queryWrapper.eq("password_hash", HashTool.encrypt(users.getPasswordHash()));
        Users user = usersService.getOne(queryWrapper);
        return Result.success(user);
    }
}
